Skip to main content

Create a Data File From a Generated Document

Fluent's Data Mode feature allows you to create a data file from a generated document. This feature is useful when you need to extract data from a generated document and use it elsewhere. This can be helpful when you have a large amount of data in your datasources and would like to create a more concise data file for use in other templates or applications.

Creating a data file from a generated document is a simple two-step process:

  1. Specify the data file to write out to
    1. Note that the data file can be embedded in office output formats (DOCX, XLSX, PPTX)
  2. Specify the mode of data to write out

Specify the Data File to Write Out To

To specify the data file you will use the Report.DataStream property. This property accepts a System.IO.FileStream object that you will write the data to. The following code snippet demonstrates how to write the data to a file:

report.DataStream = File.Create("Output_Data.xml");
tip

If you choose to embed the data file in the output document, it is not necessary to specify an output file.

Specify the Mode of Data to Write Out

To specify the data mode you will use the Report.DataMode property. This property takes in an enum (or multiple enums) that specifies the mode of data to write out. The different modes are:

  • Report.DATA_MODE.DATA: This mode writes out the value returned from the output document in each data node.
  • Report.DATA_MODE.SELECT: This mode writes out the select statement used to generate the data in each data node.
  • Report.DATA_MODE.ALL_ATTRIBUTES: This mode writes out the tag attributes for each data node.
  • Report.DATA_MODE.INCLUDE_BITMAPS: If your output document contains images from tags, this mode will include the images in the data file in uuencoded format.
  • Report.DATA_MODE.EMBED: This mode embeds the data file in the output document as data.xml. This is only valid for output formats that support embedded files (DOCX, XLSX, PPTX).

Here are a couple exmples of how to use the DataMode property:

// Sets the data mode to include the data value in each data node
report.DataMode = Report.DATA_MODE.DATA;;
// This sets the data mode to return both the data value and the select statement in each data node
// as well as embed the data file in the output document
report.DataMode = Report.DATA_MODE.EMBED | Report.DATA_MODE.DATA | Report.DATA_MODE.SELECT;

As you can see it is very simple to create a data file from a generated document using the Data Mode feature in the Fluent .NET Engine.

Below is a full code file that demonstrates how to create a data file from a generated document as well as an attached .NET project sample that you can use to test this feature:

Download .NET Project Sample

using net.windward.api.csharp;
using net.windward.xmlreport;
using System;
using System.IO;
using WindwardInterfaces.net.windward.api.csharp;

namespace DataModeSample
{
internal class DataModeSample
{
static void Main(string[] args)
{
// Initialize the engine
Report.Init();

// Open template file and create output file
FileStream template = File.OpenRead("DataModeTemplate.docx");
FileStream output = File.Create("Output/Output.docx");

// Create report process
Report report = new ReportDocx(template, output);

// Create data file stream and link it to the report data stream
FileStream dataFileStream = File.Create("Output/Output_Data.xml");
report.DataStream = dataFileStream;

// Sets the data file to contain the returned data from the tags in the DataModeTemplate
report.DataMode = Report.DATA_MODE.DATA;

// Run the report process
report.ProcessSetup();

// Open a data object to connect to our xml file and then process the data
string datasourceUrl = Path.GetFullPath("DataSource.xml");
IReportDataSource data = new SaxonDataSourceImpl(string.Format("Url={0}", datasourceUrl), null);
report.ProcessData(data, "SW");

// Finish the report process
report.ProcessComplete();

// Close out of our template file and output
data.Close();
output.Close();
template.Close();

// All finished!
Console.WriteLine("Finished generating report: " + output.Name);
Console.WriteLine("Finished generating data file: " + output.Name);
Console.ReadKey();
}
}
}